package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.CreateActivityParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.CreateActivityResult;
import com.example.lotterysystem.controller.result.FindActivityListResult;
import com.example.lotterysystem.controller.result.GetActivityDetailResult;
import com.example.lotterysystem.service.ActivityService;
import com.example.lotterysystem.service.dto.ActivityDTO;
import com.example.lotterysystem.service.dto.ActivityDetailDTO;
import com.example.lotterysystem.service.dto.CreateActivityDTO;
import com.example.lotterysystem.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;



@RestController
public class ActivityController {
    private final static Logger logger = LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(
            @Validated @RequestBody CreateActivityParam param) {
        System.out.println("createActivity");
        logger.info("创建活动,参数:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(converToCreateActivityResult(activityService.createActivity(param)));
    }

@RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> activityFindList(PageParam param){
logger.info("查询活动列表,参数:{}",JacksonUtil.writeValueAsString(param));
    PageListDTO<ActivityDTO> activityDTOPageListDTO=activityService.findActivityList(param);
return CommonResult.success(
        converToFindActivityListResult(
                activityDTOPageListDTO));
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> activityDetailFind(Long activityId){
        logger.info("查询活动详情,参数:{}",activityId);
        ActivityDetailDTO activityDetail = activityService.getActivityDetail(activityId);
        return CommonResult.success(converToGetActivityDetailResult(activityDetail));
    }

    private GetActivityDetailResult converToGetActivityDetailResult(ActivityDetailDTO activityDetail) {
if(activityDetail==null){
    throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
}
GetActivityDetailResult result=new GetActivityDetailResult();
result.setActivityName(activityDetail.getActivityName());
result.setActivityId(activityDetail.getActivityId());
result.setDescription(activityDetail.getDesc());
result.setValid(activityDetail.valid());

result.setPrizes(
        activityDetail.getPrizeDTOList().stream()
                .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTiers().getCode()))
                .map(prizeDTO -> {
                    GetActivityDetailResult.Prize prize=new GetActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setName(prizeDTO.getName());
                    prize.setDescription(prizeDTO.getDescription());
                    prize.setImageUrl(prizeDTO.getImageUrl());
                    prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setValid(prizeDTO.valid());
                    prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
                    return prize;
                }).collect(Collectors.toList())
);
result.setUsers(
        activityDetail.getUserDTOList().stream()
                .map(userDTO -> {
                    GetActivityDetailResult.User user = new GetActivityDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());
                    return user;
                }).collect(Collectors.toList())
);
return result;
    }


    private FindActivityListResult converToFindActivityListResult(PageListDTO<ActivityDTO> activityDTOPageListDTO) {
        if (null == activityDTOPageListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_LIST_ERROR);
        }
        FindActivityListResult result = new FindActivityListResult();
     result.setTotal(activityDTOPageListDTO.getTotal());
     result.setRecords(activityDTOPageListDTO.getRecords().
             stream()
             .map(activityDTO -> {
         FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
         activityInfo.setActivityName(activityDTO.getActivityName());
         activityInfo.setDescription(activityDTO.getActivityDesc());
         activityInfo.setActivityId(activityDTO.getActivityId());
         activityInfo.setValid(activityDTO.isValid());
         return activityInfo;
     }).collect(Collectors.toList()));
        return result;
    }


    private CreateActivityResult converToCreateActivityResult(CreateActivityDTO createActivityDTO) {
        if (null == createActivityDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(createActivityDTO.getActivityId());
        return result;
    }
}